<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:insert="~{include :: header('角色列表')}" />
	<th:block th:insert="~{include :: bootstrap-table-css}" />
</head>
<body class="hold-transition">
<div class="container-fluid mb-2">
	<div class="toolbar" id="toolbar" role="group">
		<form id="formId">
			<div class="form-inline">
				<a class="btn btn-success mr-1" onclick="$.operate.add()" shiro:hasPermission="role:add">
					<i class="fa fa-plus"></i> 新增
				</a>
				<input type="text" class="form-control mr-1" name="searchKey" placeholder="角色名称">
				<button type="button" class="btn btn-primary mr-1" onclick="$.table.search()">
					<i class="fa fa-search"></i>
					搜索
				</button>
				<button type="button" class="btn btn-default" onclick="$.form.reset()">
					<i class="fa fa-refresh"></i>
					重置
				</button>
			</div>
		</form>
	</div>
	<table id="bootstrap-table" class="table-striped"></table>
</div>
<th:block th:insert="~{include :: footer}" />
<th:block th:insert="~{include :: bootstrap-table-js}" />
<script th:inline="javascript">
	var editFlag = [[${@permission.hasPermi('role:edit')}]];
	var removeFlag = [[${@permission.hasPermi('role:remove')}]];
	var prefix = "/role";

	$(function() {
		var options = {
			showSearch: false,
			showRefresh: false,
			showToggle: false,
			showColumns: false,
			url: prefix + "/list.json",
			createUrl: prefix + "/add.html",
			updateUrl: prefix + "/edit.html?id={id}",
			removeUrl: prefix + "/remove.json",
			sortName: "roleSort",
			modalName: "角色",
			columns: [
				{
					field: 'roleId',
					title: '角色编号'
				},
				{
					field: 'roleName',
					title: '角色名称',
					sortable: true
				},
				{
					field: 'roleKey',
					title: '权限字符',
					sortable: true
				},
				{
					field: 'sort',
					title: '显示顺序'
				},
				{
					visible: editFlag === 'hidden' ? false : true,
					title: '是否启用',
					align: 'center',
					formatter: function (value, row, index) {
						return statusTools(row);
					}
				},
				{
					field: 'createTime',
					title: '创建时间',
					sortable: true
				},
				{
					title: '操作',
					align: 'center',
					formatter: function(value, row, index) {
						var actions = [];
						actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.roleId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
						actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.roleId + '\')"><i class="fa fa-remove"></i>删除</a> ');
						actions.push("<a class='btn btn-info btn-xs " + editFlag + "' href='javascript:void(0)' onclick='authUser(" + row.roleId + ")'><i class='fa fa-user'></i>分配用户</a>");
						return actions.join('');
					}
				}]
		};
		$.table.init(options);
	});

	/* 角色管理-分配用户 */
	function authUser(roleId) {
		var url = prefix + '/auth/list.html/' + roleId;
		layer.open({
			type: 2,
			area: ['90%', '90%'],
			maxmin: false,
			shade: 0.3,
			title: '分配用户',
			content: url,
			// 弹层外区域关闭
			shadeClose: true
		});
	}

	/* 角色状态显示 */
	function statusTools(row) {
		if (row.status == "1") {
			return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.roleId + '\')"></i> ';
		} else {
			return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.roleId + '\')"></i> ';
		}
	}

	/* 角色管理-停用 */
	function disable(roleId) {
		$.modal.confirm("确认要停用角色吗？", function() {
			$.operate.post(prefix + "/status.json", { "roleId": roleId, "status": 1 });
		})
	}

	/* 角色管理启用 */
	function enable(roleId) {
		$.modal.confirm("确认要启用角色吗？", function() {
			$.operate.post(prefix + "/status.json", { "roleId": roleId, "status": 0 });
		})
	}
</script>
</body>
</html>